#include "MainWindow.h"
#include "ui_MainWindow.h"



#include <CampaignSetting.h>
#include <CharacterClass.h>
#include <ReferenceItem.h>
#include <FileHandler.h>
#include <XmlSerializer.h>


#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    qRegisterMetaType<CampaignSetting>("CampaignSetting");
    qRegisterMetaType<CharacterClass>("CharacterClass");

    qRegisterMetaType<ValueItem>("ValueItem");
    qRegisterMetaType<RootItem>("RootItem");
    qRegisterMetaType<ReferenceItem>("RerferenceItem");

    qRegisterMetaType<Skill>("Skill");
    qRegisterMetaType<Skill>("Feat");
    qRegisterMetaType<Skill>("Special");

    FileHandler::installSerializer(new XmlSerializer(this));

    m_root = 0;
    m_model = new D20Model(this);

    ui->setupUi(this);
    ui->treeView->setModel(m_model);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actionNew_triggered()
{
    RootItem* root = FileHandler::open(":/d20/CAMPAIGN_SETTING");
    if (!root) return;

    m_root = root;
    m_model->setRootItem(m_root);
}

void MainWindow::on_actionOpen_triggered()
{
    QString filePath = QFileDialog::getOpenFileName(this, tr("Open Campaign Setting"), QDir::currentPath(), tr("Xml File (*.xml)"));
    if (filePath.isEmpty()) return;

    RootItem* root = FileHandler::open(filePath);
    if (!root) return;

    m_root = root;
    m_model->setRootItem(m_root);
}

void MainWindow::on_actionSave_As_triggered()
{
    if (!m_root) return;

    QString filePath = QFileDialog::getSaveFileName(this, tr("Save Campaign Setting"), QDir::currentPath(), tr("Xml File (*.xml)"));
    if (filePath.isEmpty()) return;

    if (FileHandler::save(m_root, filePath)) return;

    QMessageBox::information(this, tr("Save Failed!"), tr("Failed to save file as:\n%1").arg(filePath));
}

void MainWindow::on_actionSave_triggered()
{
    if (!m_root) return;

    QString filePath = m_root->filePath();
    if (!filePath.isEmpty() && FileHandler::save(m_root, filePath)) return;

    on_actionSave_As_triggered();
}

void MainWindow::on_actionClose_triggered()
{
    on_actionSave_triggered();
    close();
}

void MainWindow::on_actionAdd_Skill_triggered()
{
    QModelIndex index = m_model->addSkill();
    ui->treeView->setCurrentIndex(index);
    ui->treeView->edit(index);
}

void MainWindow::on_actionAdd_Feat_triggered()
{
    QModelIndex index = m_model->addFeat();
    ui->treeView->setCurrentIndex(index);
    ui->treeView->edit(index);
}

void MainWindow::on_actionAdd_Special_Ability_triggered()
{
    QModelIndex index = m_model->addSpecial();
    ui->treeView->setCurrentIndex(index);
    ui->treeView->edit(index);
}

void MainWindow::on_actionAdd_Character_Class_triggered()
{
    QModelIndex index = m_model->addCharacterClass();
    ui->treeView->setCurrentIndex(index);
    ui->treeView->edit(index);
}

void MainWindow::on_actionAdd_Child_Value_triggered()
{
    QModelIndex index = ui->treeView->currentIndex();
    AbstractItem* parent = m_model->indexItem(index);
    if (dynamic_cast<Skill*>(parent))
    {
        m_model->addSubSkill(parent->name());
        return;
    }

    if (dynamic_cast<Feat*>(parent)) return;
    if (dynamic_cast<Special*>(parent)) return;

    ValueItem* created = new ValueItem("New Item");
    int row = m_model->addItem(created, index);

    QModelIndex edited = m_model->index(row, AbstractItem::NameData, index);
    ui->treeView->setCurrentIndex(edited);
    ui->treeView->edit(edited);
}

void MainWindow::on_actionAdd_Sibling_Value_triggered()
{
    QModelIndex index = ui->treeView->currentIndex();
    AbstractItem* sibling = m_model->indexItem(index);
    if (!sibling) return;

    AbstractItem* item = dynamic_cast<AbstractItem*>(sibling->metaObject()->newInstance());
    if (!item) return;

    int row = m_model->addItem(item, index.parent());
    item->setData(AbstractItem::NameData, "New Item");

//    ui->treeView->setCurrentIndex(m_model->index(row, AbstractItem::NameData, index.parent()));

    QModelIndex edited = m_model->index(row, AbstractItem::NameData, index.parent());
    ui->treeView->setCurrentIndex(edited);
    ui->treeView->edit(edited);
}

void MainWindow::on_actionDelete_triggered()
{
    QModelIndex index = ui->treeView->currentIndex();
    QModelIndex parent = index.parent();
    int row = index.row();

    m_model->removeRows(row, 1, parent);
    if (m_model->rowCount(parent) <= row) row = row-1;
    if (row < 0) return;

    ui->treeView->setCurrentIndex(m_model->index(row, AbstractItem::NameData, parent));
}
